<template>
  <div>
    <div>
      <!-- <van-cell title="选择单个日期" :value="date" @click="show = true" /> -->
      <van-calendar
        v-model:show="show"
        @confirm="onConfirmData"
        :min-date="minDate"
        :max-date="maxDate"
      />
    </div>
    <img src="@/assets/img/挂号banner.jpg" width="100%" height="30%" />
    <van-tree-select
      v-model:active-id="activeId"
      v-model:main-active-index="activeIndex"
      :items="items"
      @click-item="onClickItem(activeIndex, activeId)"
    />
  </div>
</template>

<script>
import { ref, getCurrentInstance } from "vue";
import { useRouter } from "vue-router";

export default {
  setup() {
    /* eslint-disable */
    const router = useRouter(); // 获取路由实例
    const { proxy } = getCurrentInstance(); // 获取当前实例
    const activeId = ref(1);
    const activeIndex = ref(0);
    let items = ref([]);
    const today = new Date();
    const minDate = new Date(today.setDate(today.getDate())); // 从明天开始
    const maxDate = new Date(today.setDate(minDate.getDate() + 13)); // 未来两周
    const show = ref(false);
    const date = ref("");
    let paramUrl = ref("");
    const formatDate = (date) => {
      // 获取月份，由于getMonth()返回值从0开始，所以需要加1
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, "0");
      const day = String(date.getDate()).padStart(2, "0");

      return `${year}-${month}-${day}`;
    };
    const onConfirmData = (value) => {
      show.value = false;
      date.value = formatDate(value);
      console.log(" date.value", date.value);

      let param = {
        text: paramUrl.value.text,
        id: paramUrl.value.id,
        data: date.value,
      };
      router.push({ path: "/parientRegistration", query: param });
    };
    proxy.$post("/patient/findDoctorBySectionPage").then((res) => {
      items.value = res.data;
      activeIndex.value = res.data.length;
    });
    /* eslint-disable */
    const onClickItem = (activeIndex, activeId) => {
      show.value = true;
      console.log(activeIndex, activeId);
      console.log("--", items.value[activeIndex].children);
      items.value[activeIndex].children.forEach((element) => {
        if (element.id === activeId) {
          console.log(element);
          paramUrl.value = element;
          //      ;
        }
      });
      // // 使用正确的路径
    };
    return {
      items,
      activeId,
      activeIndex,
      onClickItem,
      today,
      minDate,
      maxDate,
      show,
      onConfirmData,
      date,
      paramUrl,
    };
  },
};
</script>

<style scoped>
</style>
